import featureAbility from '@ohos.ability.featureAbility'
import data_rdb from '@ohos.data.rdb'

const STORE_CONFIG = { name: "rdbStore.db" }
const SQL_CREATE_TABLE_LIST = [
    "CREATE TABLE IF NOT EXISTS NOTES (ID INTEGER PRIMARY KEY AUTOINCREMENT, TITLE TEXT NOT NULL, CONTENT TEXT NOT NULL)" //---这一条sql语句表示在库中创建一个名为NOTES的表，里面的columns包含自动自增的ID(整型),TITLE(字符串文本型),CONTENT(字符串文本型)。可添加多条sql语句
]

export default class rbdStoreModel {
    rdbStore
    createKvStore(cb) {
        if (typeof (this.rdbStore) === 'undefined') {
            let self = this;
            let context = featureAbility.getContext(); //---获取上下文
            let promise = data_rdb.getRdbStore(context, STORE_CONFIG, 1)
            promise.then((rdbStore) => {
                self.rdbStore = rdbStore;
                for (let i in SQL_CREATE_TABLE_LIST) {
                    rdbStore.executeSql(SQL_CREATE_TABLE_LIST[i], null);
                }
                console.info("xxx--- rdbStore " + 'create table.')
                cb();
            }).catch((err) => {
                console.info("xxx--- rdbStore " + err)
                cb();
            })
        } else {
            cb();
        }
    }

    insertValue(table, valueBucket) {
        this.createKvStore(() => {
            let promise = this.rdbStore.insert(table, valueBucket)
            promise.then((rows) => {
                console.info('xxx--- rdbStore.insert done ' + rows)
            })
        })
    }

    updateValue(valueBucket, table, field, value) {
        this.createKvStore(() => {
            console.info(`xxx--- rdbStore.update field = ${field} value = ${value} == ${JSON.stringify(valueBucket)}`)
            let predicates = new data_rdb.RdbPredicates(table)
            predicates.equalTo(field, value)
            this.rdbStore.update(valueBucket, predicates, function (err, rows) {
                console.info("xxx--- rdbStore.update updated row count: " + rows)
            })
        })
    }

    deleteValue(table, field, value) {
        this.createKvStore(() => {
            console.info(`xxx--- rdbStore.delete field = ${field} value = ${value}`)
            let predicates = new data_rdb.RdbPredicates(table)
            predicates.equalTo(field, value)
            this.rdbStore.delete(predicates, (err, rows) => {
                if (rows === 0) {
                    console.info("xxx--- rdbStore.delete rows: -1")
                }
                else console.info("xxx--- rdbStore.delete rows: " + rows)
            })
        })
    }

    queryValue(table, callback) { //---所有columns值查找
        this.createKvStore(() => {
            let predicates = new data_rdb.RdbPredicates(table)
            let promise = this.rdbStore.query(predicates) //---当query没有columns键值对集时默认返回所有columns
            console.info("xxx--- rdbStore query start")
            promise.then((resultSet) => {
                callback(resultSet);
            })
        })
    }

    searchValue(table, field, value, callback) { //---限定词查找
        this.createKvStore(() => {
            let predicates = new data_rdb.RdbPredicates(table)
            predicates.contains(field, value)
            let promise = this.rdbStore.query(predicates)
            console.info("xxx--- rdbStore search start")
            promise.then((resultSet) => {
                callback(resultSet);
            })
        })
    }
}
